package com.face.console.tools.config;
//

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

//
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * 授权
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests()
//                // TODO 首页所有人能看，功能页只能有权限的人才能看
//                .antMatchers("/face/api/").permitAll()// 该根目录下的资源所有人能访问
//                .antMatchers("/face/api/user/**").hasAnyRole("admin01")//admin01角色有user权限，该目录下的所有文件只有admin01级别的用户可以访问
//                .antMatchers("/face/api/menu/**").hasAnyRole("menu01");//menu01角色有菜单权限
//       // TODO 没有权限跳到登录页。或者指定路径，或者返回指定内容
//        http.formLogin().loginPage("/face/api/user/login")
//                .loginProcessingUrl("/face/api/user/login/");
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/**/*.js")
                .permitAll()
                .antMatchers("/oauth/**"
                        ,"/login/**"
                        ,"/logout/**"
                        )
                .permitAll()
                .anyRequest()
                .authenticated()
                ;
    }


    /**认证*/
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("root").password(new BCryptPasswordEncoder().encode("liudehua")).roles("admin01")
//                .and()
//                .withUser("admin").password(new BCryptPasswordEncoder().encode("liudehua")).roles("menu01","admin01");
//    }
}
